package com.icms.system.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.icms.core.base.BaseDaoAbs;
import com.icms.system.model.User;
import com.icms.system.vo.UserVo;

@Service @Transactional
public class UserService extends BaseDaoAbs<User> implements IUserService {

	@Override
	@CacheEvict(value="serviceCache", allEntries=true)
	public void add(UserVo user) {
		User entity = new User() ;
		BeanUtils.copyProperties(user, entity) ;
		this.getHibernate().add(entity) ;
	}

	@Override
	@CacheEvict(value="serviceCache", allEntries=true)
	public void delete(String id) {
		this.getHibernate().delete(User.class, id) ;
	}

	@Override
	@CacheEvict(value="serviceCache", key="#id")
	public void update(UserVo user) {
		User entity = new User() ;
		BeanUtils.copyProperties(user, entity) ;
		this.getHibernate().update(entity) ;
	}

	@Override
	@Cacheable(value="serviceCache", key="#id")
	public UserVo get(String id) {
		UserVo user = new UserVo() ;
		User entity = this.getHibernate().get(User.class, id) ;
		BeanUtils.copyProperties(entity, user) ;
		return user;
	}

	@Override
	//@Cacheable(value="serviceCache", key="#root.methodName")
	@Cacheable(value="serviceCache", key="#root.methodName")
	public List<UserVo> datagrid(UserVo user) {
		String sql = "select t.* from icms_user t" ;
		List<UserVo> list = this.getHibernate().listSQL(sql, UserVo.class, false) ;
		return list;
	}

}
